import 'package:flutter/animation.dart';
import 'package:study_demo/style/num_painter.dart';
import 'package:tile/tile.dart';
import 'package:get/get.dart';

import '../style/analysis_data_painter.dart';
import '../style/board_style.dart';
import 'BaseController.dart';

final TileStyle tileStyle = TileStyle();

initFlagPainters() async {
  tileStyle.replaceOrAddBoardPainter(tileStepNum, NumPainter());
  tileStyle.replaceOrAddBoardPainter(tileAnalysis, AnalysisDataPainter());
  tileStyle.replaceOrAddBoardPainter(tileAnalysisAINext, AnalysisDataPainter());
}

abstract class BaseChessController extends BaseController {
  final _board = Board(19).obs;
  final _blackName = "".obs;
  final _whiteName = "".obs;

  ///手顺开关状态的文本
  final textChessNum = ''.obs;

  ///是否正在显示手顺
  final isShowNum = false.obs;
  bool boardUnlock = true;
  int blackId = 0;
  int whiteId = 0;

  ///是否横屏展示
  var isLand = false;

  ///双方死子数
  final deadNum = List<int>.filled(2, 0, growable: true).obs;
  AnimationController? animationControllerBlack;
  AnimationController? animationControllerWhite;

  TileViewController tileViewController = TileViewController();

  Board get board => _board.value;

  String get blackName => _blackName.value;

  set blackName(String blackName) {
    _blackName.value = blackName;
  }

  String get whiteName => _whiteName.value;

  set whiteName(String whiteName) {
    _whiteName.value = whiteName;
  }

  @override
  void onInit() {
    super.onInit();
    textChessNum.value = 'chess_num_close'.tr;
    tileStyle.tileNum == TileNum.all;
    board.addListener(boardChanged);
    isLand = Get.width > Get.height;
  }

  ///棋盘数据改变的监听
  boardChanged(BoardFrame frame, BoardChange boardChange) {
    deadNum.assignAll(board.getDeadNum());
  }

  changeTileNum() {
    if (tileStyle.tileNum == TileNum.all) {
      tileStyle.tileNum = TileNum.end;
      textChessNum.value = 'chess_num_open'.tr;
    } else {
      tileStyle.tileNum = TileNum.all;
      textChessNum.value = 'chess_num_close'.tr;
    }
    board.postEvent('shown_num');

    isShowNum.value = tileStyle.tileNum == TileNum.all;
  }
}
